package com.wsx.ones.boots.grpc.register;

import com.wsx.ones.boots.grpc.GrpcConfig;
import com.wsx.ones.boots.grpc.GrpcSchema;
import com.wsx.ones.boots.grpc.server.OnesGrpcServer;
import com.wsx.ones.boots.service.WebServiceImpl;
import io.grpc.BindableService;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author wangshuaixin
 * @Title: com.wsx.ones.boots.grpc.GrpcStartup
 * @Description: TODO
 * @date 2019/05/21
 */
public class GrpcStartup {

    private static Properties properties;


    public static void main(String[] args) {
        OnesGrpcServer grpcServer = start(createGrpcServer(args));
        try {
            Thread.sleep(100000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static OnesGrpcServer start(OnesGrpcServer grpcServer) {
        try {
            grpcServer.startServer();

            return grpcServer;
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
        return null;
    }

    public static OnesGrpcServer createGrpcServer(String[] args) {
        GrpcConfig grpcConfig = new GrpcConfig();
        grpcConfig.setRegisterSchema(GrpcSchema.CONSUL);
        grpcConfig.setRegisterUrl("192.168.40.19:8500");

        OnesGrpcServer grpcServer = new OnesGrpcServer(grpcConfig, getService());
        boolean result = grpcServer.initialize();
        if (!result) {
            System.exit(-3);
        }
        return grpcServer;
    }

    private static List<BindableService> getService() {
        List<BindableService> services = new ArrayList<>();

        services.add(new WebServiceImpl());
        return services;
    }

}
